package com.kingyea.mobilepolice.apimanage.utils.ws;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2018/2/10 16:02
 */
public class HttpClientCallWS {

    private String namespace;
    private String methodName;
    private String wsdlLocation;
    private String soapResponseData;

    public HttpClientCallWS(String namespace, String methodName,
                            String wsdlLocation) {

        this.namespace = namespace;
        this.methodName = methodName;
        this.wsdlLocation = wsdlLocation;
    }

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        HttpClientCallWS dynamicHttpclientCall = new HttpClientCallWS(
                "http://service.test.apimanage.mobilepolice.kingyea.com/", "sayHello",
                "http://localhost:8782/services/Hello?wsdl");

        List<Object> params = new ArrayList<Object>() {{
            add("tom");
        }};
        String soapRequestData = dynamicHttpclientCall.buildRequestData(params);
        System.out.println(soapRequestData);

        int statusCode = dynamicHttpclientCall.invoke(params);
        if (statusCode == 200) {
            System.out.println("调用成功！");
            System.out.println(dynamicHttpclientCall.soapResponseData);
        } else {
            System.out.println("调用失败！错误码：" + statusCode);
        }

    }

    private int invoke(List<Object> params) throws Exception {
        PostMethod postMethod = new PostMethod(wsdlLocation);
        String soapRequestData = buildRequestData(params);

        byte[] bytes = soapRequestData.getBytes("utf-8");
        InputStream inputStream = new ByteArrayInputStream(bytes, 0,
                bytes.length);
        RequestEntity requestEntity = new InputStreamRequestEntity(inputStream,
                bytes.length, "application/soap+xml; charset=utf-8");
        postMethod.setRequestEntity(requestEntity);

        HttpClient httpClient = new HttpClient();
        int statusCode = httpClient.executeMethod(postMethod);
        soapResponseData = postMethod.getResponseBodyAsString();

        return statusCode;
    }

    private String buildRequestData(List<Object> params) {
        StringBuffer soapRequestData = new StringBuffer();
        soapRequestData.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        soapRequestData
                .append("<soapenv:Envelope " +
                        "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
                        "xmlns:ser=\"" + namespace + "\">\n");
        soapRequestData.append("<soapenv:Header/>\n");
        soapRequestData.append("<soapenv:Body>");
        soapRequestData.append("<ser:" + methodName + ">");

        for (int i = 0; i < params.size(); i++) {
            String values = params.get(i) instanceof String ? params.get(i).toString() : null;// TODO: 2018/2/10 实体对象处理。。。
            soapRequestData.append("<arg" + i + ">" + values + "</arg" + i + ">");
        }

        soapRequestData.append("</ser:" + methodName + ">");
        soapRequestData.append("</soapenv:Body>");
        soapRequestData.append("</soapenv:Envelope>");

        return soapRequestData.toString();
    }
}